//
//  PhotoDetailViewController.m
//  Iderun
//
//  Created by henry on 13-3-17.
//
//

#import "PhotoDetailViewController.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "ImageScrollView.h"
#import "EditPhotoViewController.h"
#import "Utility.h"

@interface PhotoDetailViewController ()

@end

@implementation PhotoDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
            self.edgesForExtendedLayout = UIRectEdgeNone;
        }

        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(EditPhotoContentAction:)];
    rightButton.style = UIBarButtonItemStylePlain;
    self.navigationItem.rightBarButtonItem=rightButton;
    [rightButton release];
	// Do any additional setup after loading the view.
}
-(void) EditPhotoContentAction:(id) sender{
    EditPhotoViewController *edit=[[EditPhotoViewController alloc] initWithNibName:nil bundle:nil];
    edit.file_id=self.file_id;
    edit.content.text=self.file_name;
    [self.navigationController pushViewController:edit animated:YES];
    [edit release];

}
-(void) loadImageFromURL:(NSString*) url andFile_id:(NSString*) file_id{
    self.file_id=file_id;
    for (UIView  *view in self.view.subviews) {
        [view removeFromSuperview];
    }
    AppDelegate *appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
    [MBProgressHUD  showHUDAddedTo:appdelegate.tabbar.view animated:NO];
    [self  performSelectorInBackground:@selector(downLoadImageData:) withObject:url];
}

-(void) downLoadImageData:(NSString*) url{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
           NSData *data=[NSData  dataWithContentsOfURL:[NSURL URLWithString:url]];
        [self performSelectorOnMainThread:@selector(loadImageForUI:) withObject:data waitUntilDone:NO];
    [pool  release];
}

-(void) loadImageForUI:(NSData*) data{
    if (data==nil) {
        NSString* okTitle = @"提醒";
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"图片路径配置出错" delegate:nil cancelButtonTitle:okTitle otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    UIImage *image=[UIImage imageWithData:data];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGImageGetWidth(image.CGImage), CGImageGetHeight(image.CGImage))];
    imageView.image=image;
    imageView.contentMode=UIViewContentModeScaleAspectFit;
    ImageScrollView *imageScrollView=[[ImageScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds imageView:imageView];
    imageScrollView.tag=180;
    //imageScrollView.backgroundColor=[UIColor  blueColor];
    [self.view addSubview:imageScrollView];
    [imageScrollView release];
    [imageView release];
    AppDelegate *appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
    [MBProgressHUD  hideHUDForView:appdelegate.tabbar.view animated:NO];
}
- (UIButton*) customButtom:(NSUInteger)numberOfTag withFrame:(CGRect)frame action:(SEL)action normalImage:(NSString*)normalImage lightedImage:(NSString*)lightedImage  title:(NSString *)title{
    UIButton* button;
    if (title==nil||[@"" isEqualToString:title]) {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
    }else {
        button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setTitle:title forState:UIControlStateNormal];
    }
    
    button.frame = frame;
    button.tag=numberOfTag;
    button.backgroundColor=[UIColor clearColor];
    
    if (![normalImage isEqualToString:@""]) {
        UIImage *normal_Image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:normalImage ofType:nil]];
        [button setImage:normal_Image forState:UIControlStateNormal];
    }
    
    if (![lightedImage isEqualToString:@""]) {
        UIImage *lighted_Image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:lightedImage ofType:nil]];
        [button setImage:lighted_Image forState:UIControlStateHighlighted];
        [button setImage:lighted_Image forState:UIControlStateSelected];
    }
    
    button.adjustsImageWhenHighlighted = NO;
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void) dealloc{
    [super dealloc];
}

@end
